package com.aistar.service.Impl;

import com.aistar.mapper.CompanyMapper;
import com.aistar.mapper.JobMapper;
import com.aistar.pojo.Company;
import com.aistar.pojo.Job;
import com.aistar.pojo.JobCompanyDO;
import com.aistar.pojo.JobExample;
import com.aistar.service.JobService;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class JobServiceImpl implements JobService {
    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private CompanyMapper companyMapper;


    public static void main(String[] args) {
//        ServerResponse serverResponse = new JobServiceImpl().getByJobNameAndKeyword("%工程师%",1);
        /*ServerResponse serverResponse = new JobServiceImpl().getSomeLatest();
        List<Job> jobList = (List<Job>)serverResponse.getData();
        System.out.println(jobList.size());
*/

        JobServiceImpl service = new JobServiceImpl();
        ServerResponse  serverResponse =service.getAll("工程师",null,null,8000f,10000f,null,null,null,1);




    }
    @Override // 查询最新的10 条记录
    public ServerResponse getSomeLatest() {

        /*select * from job j
        where status = 0
        order by j.job_publish_time desc
        limit 0,10*/
        JobExample example = new JobExample();
        example.createCriteria().andStatusEqualTo(0);
        example.setOrderByClause("job_publish_time desc");

        Page<Job> page = PageHelper.startPage(1,9);
        List<Job> jobList = jobMapper.selectByExample(example);


        return ServerResponse.getDateSuccess(jobList);
    }

    @Override
    public ServerResponse getByJobNameAndKeyword(String kw, Integer pageNum) {

        JobExample example = new JobExample();

        if (kw != null){
            JobExample.Criteria criteria1 = example.createCriteria();
            JobExample.Criteria criteria2 = example.createCriteria();

            criteria1.andJobNameLike(kw).andStatusEqualTo(0);
            criteria2.andJobSearchKeywordLike(kw).andStatusEqualTo(0);
            example.or(criteria1);
            example.or(criteria2);
        }else {
            example.createCriteria().andStatusEqualTo(0);
        }
        example.setOrderByClause("job_publish_time desc");//根据发布时间排序
        //分页
        Page<Job> page = PageHelper.startPage(pageNum,5);
        List<Job> jobList = jobMapper.selectByExample(example);
        PageInfo<Job> pageInfo = page.toPageInfo();

        System.out.println(jobList.size());
        for(Job job:jobList){
            System.out.println(job.getJobId()+"," + job.getJobName()+ ","+job.getJobSearchKeyword()+","+ job.getJobPublishTime());
        }
        if (jobList!=null&&jobList.size()>0)
            return ServerResponse.getDateSuccess(jobList);
        else
            return ServerResponse.getDataFailed("sorry ,no data !");
    }


/*
 1. 调用mapper  ，分页查询，获得Job集合

 2. forweach 根据job中 company_id 到CompanyMapper 根据主键查询，获得job对应的company对象

 3. 重新封装PageInfo 中的数据为 jobCompanyVOList

 4. 最后返回ServerResponse
*/
    @Override
    public ServerResponse getAll(String kw, String city, Date publishTime, Float minSalary, Float maxSalary, String workYear, String degree, String company, Integer pageNum) {
        List<JobCompanyDO> jobCompanyDOList = new ArrayList<>();


        //分页查询
        Page<Job> page = PageHelper.startPage(pageNum,5);
        List<Job> jobList =jobMapper.selectAllJobBySearch(kw,city,publishTime,minSalary,maxSalary,workYear,degree,company);
        PageInfo pageInfo = page.toPageInfo(); //pageInfo 不需要泛型

        for(Job job:jobList){
            System.out.println(job.getJobId() + "," + job.getJobName() +","+ job.getJobMinSalary() + ","+job.getJobMaxSalary()
                    + ","+","+job.getJobCity()+","+","+job.getJobDegree()+","+ job.getJobPublishTime());
        }
        //在封装company ，根据job_id 查询所属公司company

        // select * from company where company_id = ? (company_id 来源于 Job对象中的)
        for (Job job:jobList){
            Company com = companyMapper.selectByPrimaryKey(job.getCompanyId());
            jobCompanyDOList.add(new JobCompanyDO(job,com));//job，company封装到一个类里
        }
        pageInfo.setList(jobCompanyDOList);//重新封装PageInfo 中的数据为 jobCompanyVOList

        if (jobCompanyDOList != null && jobCompanyDOList.size()>0){

            return ServerResponse.getDateSuccess(pageInfo);
        }

        return ServerResponse.getDataFailed("对不起，没有找到与您匹配的岗位");
    }


    @Override
    public ServerResponse add(Job job) {
        return null;
    }

    @Override
    public ServerResponse modify(Job job) {
        return null;
    }

    @Override
    public ServerResponse modifyStatus(Job job) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id){

        Job job = jobMapper.selectByPrimaryKey(id);

        if (job == null) {
            return ServerResponse.getDataFailed("暂无岗位");
        }else {
            Company company = companyMapper.selectByPrimaryKey(job.getCompanyId());
            JobCompanyDO jobCompanyDO = new JobCompanyDO(job,company);
            return ServerResponse.getDateSuccess(jobCompanyDO);
        }
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }
}
